package com.hao.wms.mvc;

import com.hao.wms.domain.StockIncomeBill;
import com.hao.wms.query.StockIncomeBillQueryObject;
import com.hao.wms.service.IDepotService;
import com.hao.wms.service.IStockIncomeBillService;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.IOException;

public class StockIncomeBillAction extends BaseAction {
    /**
     *
     */
    private IStockIncomeBillService stockIncomeBillService;
    private IDepotService depotService;

    private StockIncomeBill stockIncomeBill = new StockIncomeBill();

    public StockIncomeBill getStockIncomeBill() {
        return stockIncomeBill;
    }

    public void setStockIncomeBill(StockIncomeBill stockIncomeBill) {
        this.stockIncomeBill = stockIncomeBill;
    }

    private StockIncomeBillQueryObject queryObject = new StockIncomeBillQueryObject();


    public StockIncomeBillQueryObject getQueryObject() {
        return queryObject;
    }

    public void setQueryObject(StockIncomeBillQueryObject queryObject) {
        this.queryObject = queryObject;
    }

    public void setStockIncomeBillService(IStockIncomeBillService stockIncomeBillService) {
        this.stockIncomeBillService = stockIncomeBillService;
    }

    public void setDepotService(IDepotService depotService) {
        this.depotService = depotService;
    }

    @InputConfig(methodName = "input")
    @RequiredPermission(value = "到货入库单列表")
    public String execute() {
        putContext(PAGERESULT, stockIncomeBillService.query(queryObject));
        putContext("depots", depotService.list());
        return LIST;
    }

    @RequiredPermission(value = "新增/修改到货入库单")
    public String input() {
        putContext("depots", depotService.list());
        if (stockIncomeBill.getId() != null) {
            stockIncomeBill = stockIncomeBillService.get(stockIncomeBill.getId());
        }
        return INPUT;
    }

    @RequiredPermission(value = "修改到货入库单")
    public String saveOrUpdate() {
        try {
            if (stockIncomeBill.getId() != null) {
                stockIncomeBillService.update(stockIncomeBill);
                addActionMessage("修改成功");
            } else {
                stockIncomeBillService.save(stockIncomeBill);
                addActionMessage("保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());
        }
        return SUCCESS;
    }

    public void prepareSaveOrUpdate() {
        if (stockIncomeBill.getId() != null) {
            stockIncomeBill = stockIncomeBillService.get(stockIncomeBill.getId());
            stockIncomeBill.setDepot(null);
            stockIncomeBill.getItems().clear();
        }
    }

    @RequiredPermission(value = "删除到货入库单")
    public String delete() throws IOException {
        stockIncomeBill = stockIncomeBillService.get(stockIncomeBill.getId());
        stockIncomeBillService.delete(stockIncomeBill);
        putResponseText("删除成功");
        return NONE;
    }

    @RequiredPermission(value = "到货入库单审核")
    public String audit() {
        if (stockIncomeBill.getId() != null) {
            stockIncomeBillService.audit(stockIncomeBill.getId());
            addActionMessage("审核成功");
        }
        return SUCCESS;
    }


}
